package ex404.manager;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Scanner;

public class ProductManager {
    private ProductInventory inventory = new ProductInventory();

    private Scanner scanner = new Scanner(System.in);

    public ProductManager() {
        // 为测试方便，先加几个产品
        inventory.addProduct(new Product("1001", "小米手机9", 3999, 10, new Date()));
        inventory.addProduct(new Product("2002", "ThinkPad 2023", 9999, 8, new Date()));
        inventory.addProduct(new Product("3003", "iPad 12", 3999, 10, new Date()));
        inventory.addProduct(new Product("2004", "ThinkBook", 6999, 10, new Date()));
    }

    public void start() {
        while (true) {
            int choice = displayMenu();
            switch (choice) {
                case 1:
                    findProduct();
                    break;
                case 2:
                    entryProduct();
                    break;
                case 3:
                    reduceProduct();
                    break;
                case 4:
                    removeProduct();
                    break;
                case 5:
                    listProduct();
                    break;
                default:
                    return;
            }
        }
    }

    private int displayMenu() {
        System.out.println("-----------------");
        System.out.println(" 1. 查询产品");
        System.out.println(" 2. 产品入库");
        System.out.println(" 3. 产品出库");
        System.out.println(" 4. 产品删除");
        System.out.println(" 5. 产品列表");
        System.out.println(" 0. 退出");
        System.out.println("-----------------");
        while (true) {
            System.out.print("你的选择: ");
            String s = scanner.nextLine();
            if (s.length() > 0 && s.charAt(0) >= '0' && s.charAt(0) <= '5') {
                return s.charAt(0) - '0';
            }
        }
    }

    private void findProduct() {
        System.out.print("输入查询的产品编号: ");
        String id = scanner.nextLine();
        Product product = inventory.getProduct(id);
        if (product == null) {
            System.out.println("产品不存在.");
        } else {
            output(product);
        }
    }

    private void entryProduct() {
        System.out.print("输入入库的产品编号: ");
        String id = scanner.nextLine();
        Product product = inventory.getProduct(id);
        if (product == null) {
            System.out.println("新增产品, 请输入: ");
            System.out.print("产品名称: ");
            String name = scanner.nextLine();
            System.out.print("产品价格: ");
            double price = scanner.nextDouble();
            System.out.print("库存数量: ");
            int quantity = scanner.nextInt();
            scanner.nextLine(); // 清除回车
            product = new Product(id, name, price, quantity, new Date());
            inventory.addProduct(product);
            System.out.println("新增产品成功.");
        } else {
            output(product);
            System.out.println("--------------------");
            System.out.print("入库数量: ");
            int qty = scanner.nextInt();
            scanner.nextLine(); // 清除回车
            product.setQuantity(product.getQuantity() + qty);
            System.out.println("产品增加数量成功.");
            output(product);
        }
    }

    private void reduceProduct() {
        System.out.print("输入出库的产品编号: ");
        String id = scanner.nextLine();
        Product product = inventory.getProduct(id);
        if (product == null) {
            System.out.println("产品不存在.");
        } else {
            output(product);
            System.out.println("--------------------");
            System.out.print("出库数量: ");
            int qty = scanner.nextInt();
            scanner.nextLine(); // 清除回车
            if (qty > product.getQuantity()) {
                System.out.println("库存数量不足.");
            } else {
                product.setQuantity(product.getQuantity() - qty);
                System.out.println("产品出库成功.");
                output(product);
            }
        }
    }

    private void removeProduct() {
        System.out.print("输入删除的产品编号: ");
        String id = scanner.nextLine();
        Product product = inventory.getProduct(id);
        if (product == null) {
            System.out.println("产品不存在.");
        } else {
            output(product);
            System.out.println("--------------------");
            System.out.print("确认要删除该产品(Y/N)? ");
            String sure = scanner.nextLine();
            if (sure.length() > 0 && sure.charAt(0) == 'Y') {
                if (inventory.removeProduct(product)) {
                    System.out.println("删除成功.");
                } else {
                    System.out.println("库存数量不为0，不能删除.");
                }
            }
        }
    }

    private void listProduct() {
        Product[] products = inventory.toArray();
        if (products.length == 0) {
            System.out.println("没有产品.");
            return;
        }

        Arrays.sort(products); // 按编号排序
        output(products);
        while (true) {
            System.out.println("1. 按名称升序/2.按价格升序/3.按库存数量降序/0.直接返回，你的选择：");
            switch (scanner.nextLine()) {
                case "1":
                    Arrays.sort(products, new ProductNameComparator());
                    break;
                case "2":
                    // 匿名内部类
                    Arrays.sort(products, new Comparator<Product>() {
                        @Override
                        public int compare(Product p1, Product p2) {
                            return Double.compare(p1.getPrice(), p2.getPrice());
                        }
                    });
                    break;
                case "3":
                    // lambda表达式
                    Arrays.sort(products, (p1, p2) -> {
                        return p2.getQuantity() - p1.getQuantity();  // p2减p1，实现降序
                    });
                    break;
                default:
                    return;
            }

            output(products); // 输出排序结果
        }
    }

    private void output(Product product) {
        System.out.printf("产品编号: %s\n", product.getId());
        System.out.printf("产品名称: %s\n", product.getName());
        System.out.printf("上架日期: %1$tY-%1$tm-%1$td\n", product.getLaunchDate());
        System.out.printf("产品价格: %.2f\n", product.getPrice());
        System.out.printf("库存数量: %d\n", product.getQuantity());
        System.out.printf("库存金额: %.2f\n", product.getQuantity() * product.getPrice());
    }

    private void output(Product[] products) {
        System.out.println("编号 | 名称 | 价格 | 库存数量 | 库存金额 | 上架日期");
        for (Product p : products) {
            System.out.printf("%s | %s | %.2f | %d | %.2f | %6$tY-%6$tm-%6$td\n",
                    p.getId(),
                    p.getName(),
                    p.getPrice(),
                    p.getQuantity(),
                    p.getPrice() * p.getQuantity(),
                    p.getLaunchDate());
        }
        System.out.println("----------------------");
        System.out.printf("合计数量: %d, 合计金额: %.2f\n", inventory.getTotalQuantity(), inventory.getTotalAmount());
    }

    // 按名称比较器，内部类
    private class ProductNameComparator implements Comparator<Product> {

        @Override
        public int compare(Product p1, Product p2) {
            return p1.getName().compareTo(p2.getName());
        }
    }
}
